package com.coderman.controller.business;

import com.coderman.business.service.IClassInfoService;
import com.coderman.business.service.IPayMentService;
import com.coderman.business.service.IStudentService;
import com.coderman.common.annotation.ControllerEndpoint;
import com.coderman.common.error.SystemException;
import com.coderman.common.model.business.ClassInfo;
import com.coderman.common.model.payMent.PayMent;
import com.coderman.common.model.student.Student;
import com.coderman.common.response.ResponseBean;
import com.coderman.common.vo.business.ClassVO;
import com.coderman.common.vo.business.PayMentVo;
import com.coderman.common.vo.student.StudentVO;
import com.coderman.common.vo.system.PageVO;
import com.wuwenze.poi.ExcelKit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

@Api(tags = "学员缴费管理")
@RestController
@RequestMapping("/pay")
public class PayMentController {

    @Autowired
    private IPayMentService payMentService;
    @Autowired
    private IStudentService studentService;

    /**
     * 用户列表
     *
     * @return
     */
    @ApiOperation(value = "缴费列表", notes = "模糊查询缴费列表")
    @GetMapping("/findList")
    @ResponseBody
    public ResponseBean<PageVO<PayMent>> findList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                  @RequestParam(value = "pageSize", defaultValue = "7") Integer pageSize,
                                                  PayMentVo payMentVo) {
        PageVO<PayMent> payList = payMentService.findList(pageNum, pageSize, payMentVo);
        return ResponseBean.success(payList);
    }

    @ApiOperation(value = "获取学员名称", notes = "获取学员名称")
    @GetMapping("/getStuNameById/{id}")
    @ResponseBody
    public ResponseBean<String> getStuNameById(@PathVariable Integer id) throws SystemException {
        String stuName = studentService.getStuNameById(id);
        if (StringUtils.isEmpty(stuName)) {
            return ResponseBean.error("获取失败");
        }
        return ResponseBean.success(stuName);
    }
    @ApiOperation(value = "获取缴费名称", notes = "获取缴费名称")
    @PostMapping("/add")
    @ResponseBody
    public ResponseBean<String> add(@RequestBody PayMent payMent) throws SystemException {
        boolean flag = payMentService.add(payMent);
        if (!flag) {
            return ResponseBean.error("保存失败");
        }

        return ResponseBean.success("保存成功");
    }

    @ApiOperation(value = "获取员工信息", notes = "获取员工信息")
    @GetMapping("/edit/{paymentid}")
    @ResponseBody
    public ResponseBean<PayMent> add(@PathVariable Integer paymentid) {
        PayMent payMent = payMentService.getById(paymentid);
        return ResponseBean.success(payMent);
    }

    /**
     * 删除班级
     *
     * @return
     */
    @ApiOperation(value = "删除班级", notes = "删除班级信息，根据班级ID")
    @DeleteMapping("/delete/{paymentid}")
    @ResponseBody
    public ResponseBean delete(@PathVariable Integer paymentid) throws SystemException {
        payMentService.deleteById(paymentid);
        return ResponseBean.success();
    }

    /**
     * 更新用户
     *
     * @param paymentid
     * @param payMent
     * @return
     */
    @ApiOperation(value = "更新班级", notes = "更新班级信息")
    @PutMapping("/update/{paymentid}")
    @ResponseBody
    public ResponseBean update(@PathVariable Integer paymentid, @RequestBody @Validated PayMent payMent) throws SystemException {
        payMentService.update(paymentid, payMent);
        //缴费成功后需要将学生状态改为在读
        StudentVO studentVO = new StudentVO();
        studentVO.setStudentStatus(1);
        studentService.update(payMent.getStudentid(),studentVO);
        return ResponseBean.success();
    }

    /**
     * 导出excel
     *
     * @param response
     */
    @ApiOperation(value = "导出excel", notes = "导出所有的excel表格")
    @PostMapping("/excel")
    @RequiresPermissions("pay:export")
    @ControllerEndpoint(exceptionMessage = "导出Excel失败", operation = "导出缴费excel")
    public void export(HttpServletResponse response) {
        List<PayMent> payMents = this.payMentService.findAll();
        ExcelKit.$Export(PayMent.class, response).downXlsx(payMents, false);
    }
}
